package cn.darkhorse.workflow.service.impl;

import cn.darkhorse.workflow.auto.entity.BpmnAlreadyDealtTask;
import cn.darkhorse.workflow.auto.service.IBpmnAlreadyDealtTaskService;
import cn.darkhorse.workflow.service.IBpmnAlreadyDealtTaskExtService;
import cn.darkhorse.workflow.vo.AlreadyDealtTaskVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: mr.wang
 * @time: 2024/1/4 9:37
 */
@Service
public class BpmnAlreadyDealtTaskExtExtServiceImpl implements IBpmnAlreadyDealtTaskExtService {
	
	@Resource
	private HistoryService historyService;
	@Resource
	private IBpmnAlreadyDealtTaskService bpmnAlreadyDealtTaskService;
	
	
	@Override
	public List<AlreadyDealtTaskVo> selectAlreadyDealtTask(String userName, String projectName, String formName, String formCode) {
		//获取审批时保存的已办信息
		LambdaQueryWrapper<BpmnAlreadyDealtTask> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(BpmnAlreadyDealtTask::getDealtUser, userName);
		if (projectName != null && !"".equals(projectName)) {
			queryWrapper.like(BpmnAlreadyDealtTask::getProjectName, projectName);
		}
		if (formName != null && !"".equals(formName)) {
			queryWrapper.like(BpmnAlreadyDealtTask::getBusinessName, formName);
		}
		if (formCode != null && !"".equals(formCode)) {
			queryWrapper.like(BpmnAlreadyDealtTask::getBusinessCode, formCode);
		}
		queryWrapper.orderByDesc(BpmnAlreadyDealtTask::getManageTime);
		List<BpmnAlreadyDealtTask> list = bpmnAlreadyDealtTaskService.list(queryWrapper);
		ArrayList<AlreadyDealtTaskVo> alreadyDealtTaskVos = new ArrayList<>();
		list.forEach(bpmnAlreadyDealtTask -> {
			AlreadyDealtTaskVo alreadyDealtTaskVo = new AlreadyDealtTaskVo();
			BeanUtils.copyProperties(bpmnAlreadyDealtTask, alreadyDealtTaskVo);
			//查询已办流程实例信息
			HistoricProcessInstance hpi = historyService
					.createHistoricProcessInstanceQuery()
					.processInstanceId(bpmnAlreadyDealtTask.getWorkflowInstanceId())
					.singleResult();
			if (hpi != null) {
				alreadyDealtTaskVo.setStartTime(hpi.getStartTime() == null ? null : dateToLocalDateTime(hpi.getStartTime()));
				alreadyDealtTaskVo.setEndTime(hpi.getEndTime() == null ? null : dateToLocalDateTime(hpi.getEndTime()));
                /*//获取所有任务节点数量
                String processDefinitionId = historyService.createHistoricProcessInstanceQuery().processInstanceId(bpmnAlreadyDealtTask.getWorkflowInstanceId()).singleResult().getProcessDefinitionId();
                BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
                Process process = bpmnModel.getProcesses().get(0);
                int flowElements = process.getFlowElements().size();
                //获取已办节点数量
                int size = historyService.createHistoricProcessInstanceQuery().processInstanceId(bpmnAlreadyDealtTask.getWorkflowInstanceId()).list().size();*/
				if (alreadyDealtTaskVo.getState() == 1) {
					alreadyDealtTaskVo.setBusinessStatus("中途结束");
				} else if (alreadyDealtTaskVo.getState() == 2) {
					alreadyDealtTaskVo.setBusinessStatus("已结束-弃审");
				} else if (alreadyDealtTaskVo.getState() == 3) {
					alreadyDealtTaskVo.setBusinessStatus("已结束-删除");
				} else if (alreadyDealtTaskVo.getEndTime() == null) {
					alreadyDealtTaskVo.setBusinessStatus("进行中");
				} else {
					alreadyDealtTaskVo.setBusinessStatus("已结束");
				}
			}
			//        WorkflowType statusEnum = WorkflowType.getStatusEnum(Long.valueOf(alreadyDealtTaskVo.getBusinessType()));
			//       alreadyDealtTaskVo.setBusinessTypeCode(statusEnum.name());
			alreadyDealtTaskVos.add(alreadyDealtTaskVo);
		});
		return alreadyDealtTaskVos;
	}
	
	
	/**
	 * 保存已办事项
	 * @param businessType
	 * @param businessId
	 * @param businessName
	 * @param businessCode
	 */
	@Override
	public  void add(String processInstanceId, String userName,String businessType, String businessId,
								   String businessName, String businessCode,Integer state) {
		BpmnAlreadyDealtTask bpmnAlreadyDealtTask = new  BpmnAlreadyDealtTask();
		bpmnAlreadyDealtTask.setDealtUser(userName);
		bpmnAlreadyDealtTask.setBusinessId(businessId);
		bpmnAlreadyDealtTask.setBusinessCode(businessCode);
		bpmnAlreadyDealtTask.setBusinessName(businessName);
		bpmnAlreadyDealtTask.setBusinessType(businessType);
		bpmnAlreadyDealtTask.setManageTime(LocalDateTime.now());
		bpmnAlreadyDealtTask.setWorkflowInstanceId(processInstanceId);
		bpmnAlreadyDealtTask.setState(state);
		//查询当前人员是否审核过该流程实例
		LambdaQueryWrapper< BpmnAlreadyDealtTask> alreadyDealtTaskQueryWrapper = new LambdaQueryWrapper<>();
		alreadyDealtTaskQueryWrapper.eq( BpmnAlreadyDealtTask::getDealtUser,bpmnAlreadyDealtTask.getDealtUser())
				.eq( BpmnAlreadyDealtTask::getWorkflowInstanceId,bpmnAlreadyDealtTask.getWorkflowInstanceId());
		List< BpmnAlreadyDealtTask> list = bpmnAlreadyDealtTaskService.list(alreadyDealtTaskQueryWrapper);
		//已存在操作记录
		boolean status;
		if (list.size()>0){
			BpmnAlreadyDealtTask bpmnAlreadyDealtTask1 = list.get(0);
			bpmnAlreadyDealtTask1.setManageTime(LocalDateTime.now());
			LambdaUpdateWrapper< BpmnAlreadyDealtTask> alreadyDealtTaskUpdateWrapper = new LambdaUpdateWrapper<>();
			alreadyDealtTaskUpdateWrapper.eq( BpmnAlreadyDealtTask::getId,bpmnAlreadyDealtTask1.getId());
			status = bpmnAlreadyDealtTaskService.update(bpmnAlreadyDealtTask1, alreadyDealtTaskUpdateWrapper);
		} else {
			//设置发起人
			LambdaQueryWrapper< BpmnAlreadyDealtTask> alreadyDealtTaskQueryWrapper1 = new LambdaQueryWrapper<>();
			alreadyDealtTaskQueryWrapper1.isNotNull( BpmnAlreadyDealtTask::getStartUser)
					.eq( BpmnAlreadyDealtTask::getWorkflowInstanceId,bpmnAlreadyDealtTask.getWorkflowInstanceId());
			List< BpmnAlreadyDealtTask> list1 = bpmnAlreadyDealtTaskService.list(alreadyDealtTaskQueryWrapper1);
			if (list1.size()>0){
				bpmnAlreadyDealtTask.setStartUser(list1.get(0).getStartUser());
			}else {
				bpmnAlreadyDealtTask.setStartUser(userName);
			}
			//保存操作记录
			status = bpmnAlreadyDealtTaskService.save(bpmnAlreadyDealtTask);
		}
		//不同意  更新该流程所有操作记录为不可查看
		if (state == 1){
			BpmnAlreadyDealtTask bpmnAlreadyDealtTask1 = new  BpmnAlreadyDealtTask();
			bpmnAlreadyDealtTask1.setState(1);
			LambdaUpdateWrapper< BpmnAlreadyDealtTask> alreadyDealtTaskUpdateWrapper1 = new LambdaUpdateWrapper<>();
			alreadyDealtTaskUpdateWrapper1.eq( BpmnAlreadyDealtTask::getWorkflowInstanceId,bpmnAlreadyDealtTask.getWorkflowInstanceId());
			bpmnAlreadyDealtTaskService.update(bpmnAlreadyDealtTask1,alreadyDealtTaskUpdateWrapper1);
		}

	}
	
	
	
	private LocalDateTime dateToLocalDateTime(Date date) {
		
		Instant instant = date.toInstant();
		ZoneId zone = ZoneId.systemDefault();
		LocalDateTime localDateTime = instant.atZone(zone).toLocalDateTime();
		return localDateTime;
	}
	
	
	
}
